package doantotnghiep.daoimpl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import doantotnghiep.dao.MedicamentBatchDao;
import doantotnghiep.model.MedicamentBatch;

public class MedicamentBatchDaoImpl implements MedicamentBatchDao{

	@Autowired
	private HibernateTemplate hibernateTemplate;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public MedicamentBatch getBatchByID(int id) {
		List<MedicamentBatch> batchs = hibernateTemplate.find("from LoSanPham where id ="+ id);
		if(batchs!=null){
			return batchs.get(0);
		}else{
			
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicamentBatch> getListBatch() {
		return hibernateTemplate.find("from MedicamentBatch order by id");
	}

	@Override
	public void saveChanged(MedicamentBatch batch) {
		this.hibernateTemplate.saveOrUpdate(batch);		
	}

	@Override
	public void deleteBatch(int id) {
		MedicamentBatch entity = this.getBatchByID(id);
		if (entity != null)
			this.hibernateTemplate.delete(entity);
	}

	@Override
	public void addBatch(MedicamentBatch batch) {
		this.hibernateTemplate.saveOrUpdate(batch);
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	public List<MedicamentBatch> getListBatchByExpirationDate(String candiateDate) {
	
	    List<MedicamentBatch>list = this.hibernateTemplate.find("from MedicamentBatch where expirationDate < "+candiateDate);// where NgayHetHan
	    return list;
	}
}
